package edu.hubu.talentmanagementsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.CaseFormat;
import edu.hubu.talentmanagementsystem.mapper.FriendMapper;
import edu.hubu.talentmanagementsystem.mapper.context.MapperContext;
import edu.hubu.talentmanagementsystem.model.ext.Friend;
import edu.hubu.talentmanagementsystem.service.IFriendService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import edu.hubu.talentmanagementsystem.common.EncodeOperation;
import edu.hubu.talentmanagementsystem.common.EncodeParam;

import java.util.List;
import java.util.Optional;

/**
 * @author moonlan
 * date 2021/3/2 下午8:45
 */
@SuppressWarnings(value = "unused")
@Service
@Transactional(rollbackFor = Exception.class)
public class FriendServiceImpl extends ServiceImpl<FriendMapper, Friend> implements IFriendService {

    private final MapperContext context;

    public FriendServiceImpl(MapperContext context) {this.context = context;}

    @Override
    public boolean deleteById(Integer id) {
        return context.getFriendMapper().deleteById(id) > 0;
    }

    @Override
    @EncodeOperation
    public Optional<Friend> insert(@EncodeParam Friend record) {
        int insert = context.getFriendMapper().insert(record);
        if (insert > 0) {
            return Optional.of(context.getFriendMapper().selectById(record.primaryKeyValue()));
        }
        return Optional.empty();
    }

    @Override
    public Optional<Friend> selectById(Integer id) {
        QueryWrapper<Friend> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<Friend> lambdaQueryWrapper = getColumnWrapper(wrapper.lambda()).eq(Friend::getFriendId, id);
        return Optional.of(context.getFriendMapper().selectOne(lambdaQueryWrapper));
    }

    @Override
    public List<Friend> selectAll(String order) {
        QueryWrapper<Friend> wrapper = new QueryWrapper<>();
        if (!StringUtils.hasText(order)) {
            LambdaQueryWrapper<Friend> select = getColumnWrapper(wrapper.lambda());
            return context.getFriendMapper().selectList(select);
        }
        wrapper.orderByAsc(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, order));
        LambdaQueryWrapper<Friend> select = getColumnWrapper(wrapper.lambda());
        return context.getFriendMapper().selectList(select);
    }

    @Override
    public Page<Friend> selectAllPaged( Integer currentPage,
             Integer pageSize, String order) {
        QueryWrapper<Friend> wrapper = new QueryWrapper<>();
        Page<Friend> page = new Page<>(currentPage, pageSize);
        if (!StringUtils.hasText(order)) {
            LambdaQueryWrapper<Friend> select = getColumnWrapper(wrapper.lambda());
            return context.getFriendMapper().selectPage(page, select);
        }
        wrapper.orderByAsc(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, order));
        LambdaQueryWrapper<Friend> select = getColumnWrapper(wrapper.lambda());
        return context.getFriendMapper().selectPage(page, select);
    }

    private LambdaQueryWrapper<Friend> getColumnWrapper(LambdaQueryWrapper<Friend> wrapper) {
        return wrapper.select(
                Friend::getStaffId
                    ,
                Friend::getFriendId
                    ,
                Friend::getStaffFriendId
                    ,
                Friend::getStaffFriendGroup
        );
    }

    @Override
    public Page<Friend> selectLikePaged( Integer currentPage,
             Integer pageSize, Friend record, String order) {
        Page<Friend> page = new Page<>(currentPage, pageSize);
        QueryWrapper<Friend> wrapper = new QueryWrapper<>();
        if (!StringUtils.hasText(order)) {
            return context.getFriendMapper().selectPage(page, getColumnWrapper(getQueryWrapper(wrapper, record)));
        }
        wrapper.orderByAsc(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, order));
        return context.getFriendMapper().selectPage(page, getColumnWrapper(getQueryWrapper(wrapper, record)));
    }

    private LambdaQueryWrapper<Friend> getQueryWrapper(QueryWrapper<Friend>wrapper, Friend record) {
        LambdaQueryWrapper<Friend> lambda = wrapper.lambda();
        if (record.getStaffId() != null) {
            lambda = lambda.eq(Friend::getStaffId,record.getStaffId());
        }
        if (record.getFriendId() != null) {
            lambda = lambda.eq(Friend::getFriendId,record.getFriendId());
        }
        if (record.getStaffFriendId() != null) {
            lambda = lambda.eq(Friend::getStaffFriendId,record.getStaffFriendId());
        }
        if (record.getStaffFriendGroup() != null) {
            lambda = lambda.like(Friend::getStaffFriendGroup,record.getStaffFriendGroup ());
        }
        return lambda;
    }

    public int count(Friend record) {
        return context.getFriendMapper().selectCount(getQueryWrapper(new QueryWrapper<>(), record));
    }

    @Override
    @EncodeOperation
    public Optional<Friend> updateById(Integer id, @EncodeParam Friend record) {
        record.setFriendId(id);
        int update = context.getFriendMapper().updateById(record);
        if (update > 0) {
            QueryWrapper<Friend> wrapper = new QueryWrapper<>();
            LambdaQueryWrapper<Friend> lambdaQueryWrapper = getColumnWrapper(wrapper.lambda()).eq(Friend::getFriendId, id);
            return Optional.of(context.getFriendMapper().selectOne(getColumnWrapper(lambdaQueryWrapper)));
        }

        return Optional.empty();
    }
}
